﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using MathsFunctions;

namespace CONSaver_2
{
    class FileScan
    {

        //Xbox 360 CON file Functions

        internal string GetTypeFile(string path)
        {
            string FileType, FileTypeRead;

            int offset = 836;

            BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open));
            br.BaseStream.Seek(offset, SeekOrigin.Begin);
            FileTypeRead = String.Format("{0:X}", System.Net.IPAddress.HostToNetworkOrder(br.ReadInt32()));
            br.Close();

            switch (FileTypeRead)
            {
                case "1":
                    FileType = "Saved Game";
                    break;
                case "10000":
                    FileType = "Profile";
                    break;
                case "D0000":
                    FileType = "Arcade";
                    break;
                case "9000":
                    FileType = "Avatar Item";
                    break;
                case "40000":
                    FileType = "Cache";
                    break;
                case "2000000":
                    FileType = "Community Game";
                    break;
                case "80000":
                    FileType = "Game Demo";
                    break;
                case "20000":
                    FileType = "Gamer Picture";
                    break;
                case "A0000":
                    FileType = "Game Title";
                    break;
                case "C0000":
                    FileType = "Game Trailer";
                    break;
                case "400000":
                    FileType = "Game Video";
                    break;
                case "4000":
                    FileType = "Installed Game";
                    break;
                case "B0000":
                    FileType = "Installer";
                    break;
                case "2000":
                    FileType = "IPTV Pause Buffer";
                    break;
                case "F0000":
                    FileType = "License Store";
                    break;
                case "2":
                    FileType = "Market Place Content";
                    break;
                case "100000":
                    FileType = "Movie";
                    break;
                case "300000":
                    FileType = "Music Video";
                    break;
                case "500000":
                    FileType = "Podcast Video";
                    break;
                case "3":
                    FileType = "Publisher";
                    break;
                case "50000":
                    FileType = "Storage Download";
                    break;
                case "30000":
                    FileType = "Thematic Skin";
                    break;
                case "200000":
                    FileType = "TV";
                    break;
                case "90000":
                    FileType = "Video";
                    break;
                case "600000":
                    FileType = "Viral Video";
                    break;
                case "70000":
                    FileType = "Xbox Download";
                    break;
                case "5000":
                    FileType = "Xbox Original Game";
                    break;
                case "60000":
                    FileType = "Xbox Saved Game";
                    break;
                case "1000":
                    FileType = "Xbox 360 Title";
                    break;
                //case "5000": 
                //FileType = "Xbox Title";
                //break;
                case "E0000":
                    FileType = "XNA";
                    break;
                default:
                    FileType = "Unknown";
                    break;
            }

            return FileType;

        }

        private void RecursiveScan(string pathToScan)
        {
            DirectoryInfo Dir = new DirectoryInfo(pathToScan);

            foreach (FileInfo fileFound in Dir.GetFiles("*.*"))
            {
                //check if CON file
                // AddToList(fileFound.FullName);
            }

            foreach (DirectoryInfo DirFound in Dir.GetDirectories())
            {
                RecursiveScan(DirFound.FullName);
            }

        }

        internal string GetFilename(string Path)
        {
            string SaveFileByte = null;
            string SaveFile = null;
            int offset = 1042;
            try
            {
                BinaryReader br = new BinaryReader(File.Open(Path, FileMode.Open));

                //get game name

                while (SaveFileByte != "0")
                {

                    br.BaseStream.Seek(offset, SeekOrigin.Begin);
                    SaveFileByte = String.Format("{0:X}", br.ReadByte());

                    SaveFile = SaveFile + SaveFileByte;

                    offset += +2;
                }

                br.Close();
                SaveFile = Conversions.HexString2Ascii(SaveFile);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return SaveFile;
        }

        internal string GetFlaggedArea(string Path)
        {
            long FlagFind;
            string Flag = null;

            BinaryReader br = new BinaryReader(File.Open(Path, FileMode.Open));
            br.BaseStream.Seek(556, SeekOrigin.Begin);
            FlagFind = System.Net.IPAddress.HostToNetworkOrder(br.ReadInt64());

            Flag = String.Format("{0:X}", FlagFind);
            br.Close();
            return Flag;

        }

        internal string GetGameName(string Path, string FileType)
        {
            X360code x360codeFunctions = new X360code();

            string GameNameByte = null;
            string GameName = null;
            int offset = 5778;

            if (FileType == "Profile")
            {
                GameName = x360codeFunctions.ExtractProfileName(Path);
            }
            else
            {
                try
                {
                    BinaryReader br = new BinaryReader(File.Open(Path, FileMode.Open));
                    br.BaseStream.Seek(556, SeekOrigin.Begin);
                    //get game name
                    while (GameNameByte != "0")
                    {
                        br.BaseStream.Seek(offset, SeekOrigin.Begin);
                        GameNameByte = String.Format("{0:X}", br.ReadByte());

                        GameName = GameName + GameNameByte;

                        offset += +2;
                    }
                    br.Close();

                    GameName = Conversions.HexString2Ascii(GameName);
                     
                }
                catch (Exception ex)
                {

                }
            }
        return GameName;
        }

        internal string GetDeviceID(string Path)
        {
            string DeviceIDRead = null;
            string DeviceIDByte = null;
            try
            {

                BinaryReader br = new BinaryReader(File.Open(Path, FileMode.Open));


                int offset = 1021;
                //get Device ID
                while (DeviceIDByte != "0")
                {
                    br.BaseStream.Seek(offset, SeekOrigin.Begin);
                    DeviceIDByte = String.Format("{0:X}", br.ReadByte());
                    //if (DeviceIDByte.Length == 1)
                    //{
                    //    DeviceIDByte = "0" + DeviceIDByte;
                    //}

                    //    if (DeviceIDByte.Length == 1)
                    //        DeviceIDByte = "0" + DeviceIDByte;

                    DeviceIDRead = DeviceIDRead + DeviceIDByte;

                    offset += +1;
                }
                br.Close();
            }
            catch (Exception ex)
            {

            }

            return DeviceIDRead;

        }

        internal string GetConsoleID(string Path)
        {
            string ConsoleIDRead = null;
            string ConsoleIDByte = null;

            try
            {

                BinaryReader br = new BinaryReader(File.Open(Path, FileMode.Open));

                int offset = 876;
                //get Device ID

                for (int offsetcount = 0; offsetcount <= 5; offsetcount++)
                {
                    br.BaseStream.Seek(offset, SeekOrigin.Begin);
                    ConsoleIDByte = String.Format("{0:X}", br.ReadByte());
                    ConsoleIDRead = ConsoleIDRead + ConsoleIDByte;
                    offset++;

                }
                br.Close();
            }
            catch
            {

            }

            return ConsoleIDRead;
        }
    }
}
